/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.jefb.entity.DestinationEntity;
import org.jefb.sec.entity.ReceiverSession;
import org.jefb.sec.entity.SenderSession;
import org.jefb.service.IAppInitService;
import org.jefb.service.ITransSessionStoreService;
import org.jefb.util.JefbUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class TransSessionStoreService implements ITransSessionStoreService {
	
	private static Logger log = LoggerFactory.getLogger(FileRegisterService.class);

	//services
	
	private Map<String, ReceiverSession> receiverSessionStore = new HashMap<String, ReceiverSession>();;
	private Map<String, SenderSession> senderSessionStore = new HashMap<String, SenderSession>();;
	private Integer receiverSessionStoreMaxSize;
	private Integer senderSessionStoreMaxSize;
	private Integer receiverSessionTTL = 60000;
	private Integer senderSessionTTL = 60000;

	public void init() {

	}

	public SenderSession getSenderSession(DestinationEntity destination) {
		return senderSessionStore.get(destination.getAddress());
		// if receiver dosn't exist, try to init one

	}

	public void cleanReceiverSessionStore() {
		for (ReceiverSession receiverSession : receiverSessionStore.values()) {
			if ((System.currentTimeMillis() - receiverSession.getCreationTime()
					.getTime()) > receiverSessionTTL) {
				receiverSessionStore.remove(receiverSession.getSessionId());
			}
		}
	}

	public void cleanSenderSessionStore() {
		for (SenderSession senderSession : senderSessionStore.values()) {
			if ((System.currentTimeMillis() - senderSession.getCreationTime()
					.getTime()) > senderSessionTTL) {
				senderSessionStore.remove(senderSession.getDestId());
			}
		}
	}

	public String registerReceiverSession(ReceiverSession secureSession) {
		// TODO check max session store size
		String sessionId = JefbUtils.generateSessionId();
		secureSession.setSessionId(JefbUtils.generateSessionId());
		if (!receiverSessionStore.containsKey(secureSession.getSenderAgentId())) {
			receiverSessionStore.put(secureSession.getSenderAgentId(), secureSession);
		} else {
			
			// TODO session already exists
		}
		return sessionId;
	}

	public void unregisterReceiverSession(String agentId) {
		receiverSessionStore.remove(agentId);
	}

	public boolean receiverSessionExists(String agentId) {
		return receiverSessionStore.containsKey(agentId);
	}

	public ReceiverSession getReceiverSession(String agentId) {
		return receiverSessionStore.get(agentId);
	}

	public void registerSenderSession(SenderSession secureSession) {
		// TODO check max session store size
		if (!senderSessionStore.containsKey(secureSession.getDestId())) {
			senderSessionStore.put(secureSession.getDestId(), secureSession);
		} else {
			// TODO session already exists
		}
	}

	public boolean senderSessionExists(String agentId) {
		return senderSessionStore.containsKey(agentId);
	}

	public void unregisterSenderSession(String agentId) {
		senderSessionStore.remove(agentId);
	}

	public Integer getReceiverSessionStoreMaxSize() {
		return receiverSessionStoreMaxSize;
	}

	public void setReceiverSessionStoreMaxSize(
			Integer receiverSessionStoreMaxSize) {
		this.receiverSessionStoreMaxSize = receiverSessionStoreMaxSize;
	}

	public Integer getSenderSessionStoreMaxSize() {
		return senderSessionStoreMaxSize;
	}

	public void setSenderSessionStoreMaxSize(Integer senderSessionStoreMaxSize) {
		this.senderSessionStoreMaxSize = senderSessionStoreMaxSize;
	}

	public Integer getReceiverSessionTTL() {
		return receiverSessionTTL;
	}

	public void setReceiverSessionTTL(Integer receiverSessionTTL) {
		this.receiverSessionTTL = receiverSessionTTL;
	}

	public Integer getSenderSessionTTL() {
		return senderSessionTTL;
	}

	public void setSenderSessionTTL(Integer senderSessionTTL) {
		this.senderSessionTTL = senderSessionTTL;
	}

}
